<?php
/**
 * Created by PhpStorm.
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Common\Service\RedisService;
use App\Jobs\Visits;

class WebVisits
{
    /**
     * 检测web端访问数量
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $ip = $request->ip();
        $isHave = RedisService::get($ip);
        if (!$isHave) {
            //存入redis
            RedisService::set($ip, $ip);

            //分发队列（一分钟后存入数据库）
            Visits::dispatch($ip)->delay(now()->addMinutes(1));
        }

        return $next($request);
    }
}